﻿using JYH.Business;
using JYH.Data;
using JYH.DataAccess;
using JYH.Model;
using JYH.Model.API;
using Microsoft.AspNetCore.Cors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace JYH.Web.MES.Controllers
{

    [EnableCors("AllowAll")]
    public class EXTController : ApiController
    {
        /// <summary>
        /// 获取条码信息
        /// </summary>
        /// <param name="info">物料条码集合</param>
        /// <returns></returns>
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public HttpResponseMessage GetMaterialBarcodeInfo([FromBody] List<BasBarcodeInfo> infos)
        {
            ResponseString<BasBarcodeInfo> json = new ResponseString<BasBarcodeInfo>();
            json.Code = 200;
            json.Message = string.Empty;
            json.Items = new List<BasBarcodeInfo>();
            try
            {
                string NO = "'',";
                foreach (var item in infos) 
                {
                    NO += "'"+item.BarcodeNO+"',";
                }
                NO=NO.Substring(0, NO.Length-1);
                Database db = DB.D();
                Sql sql = new Sql();
                sql.Append(@";Select 
                                BasBarcodeInfo.BarcodeNO,
                                BasBarcodeInfo.BatchNO,
                                BasBarcodeInfo.Qty,
                                BasMaterialInfo.MaterialNO,
                                BasMaterialInfo.MaterialName,
                                BasMaterialInfo.MaterialDesc
                                From BasBarcodeInfo
                                Inner JOIn BasMaterialInfo
                                On BasMaterialInfo.MaterialID=BasBarcodeInfo.MaterialID");
                sql.Where(" BasBarcodeInfo.BarcodeNO IN  (" + NO + ")");
                sql.OrderBy("BasBarcodeInfo.BarcodeNO");
                json.Items = db.Fetch<BasBarcodeInfo>(sql);

            }
            catch (Exception ex)
            {
                json.Message = ex.Message;
                json.Code = 500;
            }
           var response = Request.CreateResponse(HttpStatusCode.OK, json);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return response;
        }

        /// <summary>
        /// 生成新标签
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public HttpResponseMessage SaveMaterialBarcodeInfo([FromBody] BasBarcodeInfo info)
        {
            ResponseString<BasBarcodeInfo> json = new ResponseString<BasBarcodeInfo>();
            json.Code = 200;
            json.Message = string.Empty;
            json.Items = new List<BasBarcodeInfo>();
            var response = Request.CreateResponse(HttpStatusCode.OK, json); ;
            try
            {
                Database db = DB.D();
                Sql sql = new Sql();
                BasBarcodeInfo barcode = new BasBarcodeInfo();
                BasMaterialInfo material = new BasMaterialInfo();
                string MaterialNO=string.Empty;
                if (!string.IsNullOrEmpty(info.BarcodeNO))
                {
                    sql = new Sql();
                    sql.Append(@";Select 
                                BasBarcodeInfo.BarcodeNO,
                                BasBarcodeInfo.BatchNO,
                                BasBarcodeInfo.Qty,
                                BasMaterialInfo.MaterialNO,
                                BasMaterialInfo.MaterialName,
                                BasMaterialInfo.MaterialDesc
                                From BasBarcodeInfo
                                Inner JOIn BasMaterialInfo
                                On BasMaterialInfo.MaterialID=BasBarcodeInfo.MaterialID");
                    sql.Where(" BasBarcodeInfo.BarcodeNO ='"+ info .BarcodeNO+ "'");
                    barcode = db.FirstOrDefault<BasBarcodeInfo>(sql);
                    if (barcode == null) 
                    {
                        json.Message = "旧条码不存在";
                        json.Code = 500;
                        response = Request.CreateResponse(HttpStatusCode.OK, json);
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        return response;
                    }
                   

                }
                if (barcode == null || string.IsNullOrEmpty(info.BarcodeNO))
                {
                    if (string.IsNullOrEmpty(info.MaterialNO))
                    {
                        json.Message = "物料料号不能为空";
                        json.Code = 500;
                        response = Request.CreateResponse(HttpStatusCode.OK, json);
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        return response;
                    }
                    sql = new Sql();
                    sql.Append("Select BasMaterialInfo.* From BasMaterialInfo");
                    sql.Where("BasMaterialInfo.MaterialNO='" + info.MaterialNO + "'");
                    material= db.FirstOrDefault<BasMaterialInfo>(sql);
                    if (material == null)
                    {
                        json.Message = "物料料号不正确";
                        json.Code = 500;
                        response = Request.CreateResponse(HttpStatusCode.OK, json);
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        return response;
                    }

                }
                if (string.IsNullOrEmpty(info.BatchNO)&&string.IsNullOrEmpty(info.BarcodeNO))
                {
                    json.Message = "物料批号不能为空";
                    json.Code = 500;
                    response = Request.CreateResponse(HttpStatusCode.OK, json);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    return response;
                }
                if (info.Qty<1)
                {
                    json.Message = "物料包装数量不能小于1";
                    json.Code = 500;
                    response = Request.CreateResponse(HttpStatusCode.OK, json);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    return response;
                }
                BasBarcodeInfo obj = new BasBarcodeInfo();
                string Msg=string.Empty;
                if (barcode != null)
                {
                    obj = Framework.ObjectReflection.DeepCloneObject(barcode);
           
                }
                else
                {
                    obj.MaterialID=material.MaterialID;
                    obj.BatchNO = info.BatchNO;
                }
                obj.BarcodeID = Guid.NewGuid().ToString();
                obj.BarcodeNO = SysFormNOInfoBLL.GetLastFormNO("BarcodeNO", out Msg);
        
                obj.Qty = info.Qty;
                db.Execute(BusinessTool.GetInsertSql(obj));
                json.Items.Add(obj);

            }
            catch (Exception ex)
            {
                json.Message = ex.Message;
                json.Code = 500;
            }
             response = Request.CreateResponse(HttpStatusCode.OK, json);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return response;
        }
    }

    public class ResponseString<T> where T : class, new()
    {
        public int Code { get; set; } = 200;
        public string Message { get; set; }=string.Empty;

        public List<T> Items { get; set; }=new List<T>();
    }
}